/*
 * create:2015年11月22日 上午11:05:16
 * author:wangds  
 */
package com.cxkj.shop.endpoint;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.wangds.comp.endpoint.http.json.HttpJsonInteractionControllerEndPoint;
import net.wangds.comp.proc.http.WriteNoCacheHeader2HttpResponseProcessor;
import net.wangds.utilities.sec.AesUtils;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

/**
 * 输入输出加密.
 * @author wangds
 *
 */
public class HttpJsonIaCtrlEndPoint<R, A> extends HttpJsonInteractionControllerEndPoint<R, A> {

    /**
     * HttpJsonIaCtrlEndPoint.java.
     * @author wangds 2015年11月23日 下午1:02:12.
     */
    private static final String DEFAULT_KEY = "31323334353637383132333435363738";//32位
    private static final String IV = DEFAULT_KEY;
    /**
     * 是否在应答中写入no cache头.
     */
    private boolean noCache = true;

    
    /**
     * The getter method of the property noCache.
     * @author wangds 2015年12月5日 下午5:08:56.
     * @return the noCache.
     */
    public boolean isNoCache() {
        return noCache;
    }

    /**
     * The setter method of the property noCache.
     * @param thenoCache the noCache to set
     * @author wangds 2015年12月5日 下午5:08:56.
     */
    public void setNoCache(boolean noCache) {
        this.noCache = noCache;
    }
    
 

    /*
     * @see eastone.endpoint.AbstractJsonHttpEndPoint#loadRequestBody(javax.servlet.http.HttpServletRequest)
     * @author wangds 2015年11月22日 上午11:06:16.
     */
    @Override
    protected byte[] loadRequestBody(HttpServletRequest req) {
        byte[] res= super.loadRequestBody(req);
        try {
            byte[] key = Hex.decodeHex(DEFAULT_KEY.toCharArray());
            byte[] iv = Hex.decodeHex(IV.toCharArray());
            this.getLogger().debug("req body:"+new String(res));
            res = Hex.decodeHex(new String(res).toCharArray());
            res = AesUtils.decrypt(res, key, iv);
            this.getLogger().debug("req body decrypt:"+new String(res));
        } catch (DecoderException e) {
            this.getLogger().error(e.getLocalizedMessage(), e);
        }
        
        return res;
    }
    /*
    public static void main(String[] args) throws Exception {
        byte[] key = Hex.decodeHex(DEFAULT_KEY.toCharArray());
        byte[] iv = Hex.decodeHex(IV.toCharArray());
        String t = "da464c1e1a086c4e5e9491117140403d2f83ab782dc8171c8ece6ea3a42761150c5e3507af1bb53d8b5db0461d8d4d425242e660557f99b82149694c01a3213ed7e9fe506feb95d7c36039fe32b85c1ae1e2026d53a9b0b7eb8eecd1146aaa15a98fc28066e5cb3a0171d80e6173e003fd029f727bd8d4dc0276faa06d6f7fc49a7fb2bad7ce684b8f37f5d14fabb2c8553147fd7ea2c0d1c9a2f14c36839ba7";
        System.out.println(t.length());
        System.out.println(t.toCharArray());
        System.out.println(Hex.encodeHex(Hex.decodeHex(t.toCharArray())).length);
        t= new String(AesUtils.decrypt(Hex.decodeHex(t.toCharArray()), key, iv));
        System.out.println(t);
    }
    */
    /*
     * @see eastone.endpoint.AbstractJsonHttpEndPoint#writeResponse(javax.servlet.http.HttpServletResponse, byte[])
     * @author wangds 2015年11月22日 上午11:06:33.
     */
    @Override
    protected void writeResponse(HttpServletResponse resp, byte[] output) {
        if(noCache ){
            new WriteNoCacheHeader2HttpResponseProcessor(resp).process();
            resp.addHeader("Access-Control-Allow-Origin", "*");
        }
        try {
            byte[] key = Hex.decodeHex(DEFAULT_KEY.toCharArray());
            byte[] iv = Hex.decodeHex(IV.toCharArray());
            output = AesUtils.encrypt(output, key, iv);
            output = Hex.encodeHexString(output).getBytes();
        } catch (DecoderException e) {
            this.getLogger().error(e.getLocalizedMessage(), e);
        }
        super.writeResponse(resp, output);
    }

}
